package com.mftour.moudles.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mftour.common.Constant;
import com.mftour.common.utils.ExcelUtil;
import com.mftour.common.utils.MyDateUtils;
import com.mftour.moudles.entity.User;
import com.mftour.moudles.service.TestService;
import com.mftour.moudles.service.UserService;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

@Service
public class TestServiceImpl implements TestService {
	@Autowired
	UserService userSer;

	@Override
	public void save1() {
		User user = new User();
		user.setUserName("zdm1");
		user.setLoginId("zdmlogin1");
		userSer.insertSelectiveException(user);
		User u = new User();
		u.setLiveStatus("123");
		userSer.insertSelectiveException(u);
	}

	@Override
	public boolean save2() {
		User user = new User();
		user.setUserName("zdm2");
		user.setLoginId("zdmlogin2");
		userSer.insertSelectiveManual(user);
		User u = new User();
		u.setLiveStatus("123");
		return userSer.insertSelectiveManual(u);
	}

	/**
	 * 读取excel文件当做模板，填入相应内容
	 * @return
	 */
	@Override
	public String exportToExcel() {
		String destPath = new Date().getTime() + ".xlsx";
		String tplPath = Constant.TEMPLETE_PATH + "区域电池组告警信息统计表.xlsx";
		ExcelUtil excel = new ExcelUtil();
		excel.setSrcPath(tplPath);
		excel.setDesPath(destPath);
		excel.setSheetName("区域电池组告警信息统计表");
		excel.getSheet();
		excel.setCellStrValue(1, 8, "导出时间:" + MyDateUtils.getDateString(new Date()));
		int rowIndex = 4;
		XSSFCellStyle cellStyle = excel.getCellStyle(rowIndex, 0);
		int firstRow = rowIndex;
		int lastRow = 0;
		List<User> userList = new ArrayList<User>();
		for (int i = 0; i < userList.size(); i++) {
			User u = userList.get(i);
			// 合并单元格
			if (u.getUserStatus().equals(u.getUserStatus())) {
				lastRow = rowIndex + i;
				excel.sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, 0, 0));
				firstRow = lastRow + 1;
			}
			Row row = excel.createRow(rowIndex + i);
			// excel.createSetCell(row, 0, wa.getCompanyName3(), cellStyle);
			// excel.createSetCell(row, 1, wa.getDistrict(), cellStyle);
			// excel.createSetCell(row, 2, wa.getNum() + "", cellStyle);
			// excel.createSetCell(row, 3, wa.getLossElectricityNum() + "",
			// cellStyle);
			// excel.createSetCell(row, 4, wa.getLossElectricityPercent() + "%",
			// cellStyle);
			// excel.createSetCell(row, 5, (wa.getCellTemLowNum() +
			// wa.getCellTemHighNum()) + "", cellStyle);
			// excel.createSetCell(row, 6,
			// (new BigDecimal(wa.getCellTemHighPercent()).add(new
			// BigDecimal(wa.getCellTemLowPercent()))) + "%",
			// cellStyle);
			// excel.createSetCell(row, 7, (wa.getGenVolHighNum() +
			// wa.getGenVolLowNum()) + "", cellStyle);
			// excel.createSetCell(row, 8,
			// (new BigDecimal(wa.getGenVolHighPercent()).add(new
			// BigDecimal(wa.getGenVolLowPercent()))) + "%",
			// cellStyle);
			// excel.createSetCell(row, 9, (wa.getEnvTemHighNum() +
			// wa.getEnvTemLowNum()) + "", cellStyle);
			// excel.createSetCell(row, 10,
			// (new BigDecimal(wa.getEnvTemHighPercent()).add(new
			// BigDecimal(wa.getEnvTemLowPercent()))) + "%",
			// cellStyle);
			// excel.createSetCell(row, 11, wa.getSocLowNum() + "", cellStyle);
			// excel.createSetCell(row, 12, wa.getSocLowPercent() + "%",
			// cellStyle);
		}
		excel.exportToNewFile();
		return destPath;
	}
	
	

	/**
	 * 直接通过jxls导出，excel模板文件中设置好即可
	 * @param userList
	 * @return
	 */
	@Override
	public String exportToExcel(List<User> userList) {
		// String tplPath =
		// "E:/workspace/station/src/main/resources/templete/区域级实时告警信息报表.xlsx";
		String tplPath = Constant.TEMPLETE_PATH + "区域级实时告警信息报表.xlsx";
		String destPath = new Date().getTime() + ".xlsx";
		Map beanParams = new HashMap();
		beanParams.put("stationWarnList", userList);
		beanParams.put("exportDateTime", MyDateUtils.getDateString(new Date()));
		XLSTransformer former = new XLSTransformer();
		try {
			former.transformXLS(tplPath, beanParams, destPath);
		} catch (ParsePropertyException e) {
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return destPath;
	}
}
